package XYC;

import java.awt.Color;
import java.awt.geom.Point2D;
import robocode.ScannedRobotEvent;
import robocode.HitRobotEvent;
import robocode.HitByBulletEvent;

/**
 * Creates a robot that try to beat other sample robots.
 * @author Yichi Xu
 * */
public class YichiSpecialBot extends YichiRobot {
  /**
   * The main method that run at the beginning of every battle.
   * */
  public void run() {
    setColors(Color.gray,Color.green, Color.orange);
    double height = getBattleFieldHeight();
    double width = getBattleFieldWidth();
    Point2D[] locs = {
        new Point2D.Double(getWidth() / 2, height - getHeight() / 2),//upper left
        new Point2D.Double(width - getWidth() / 2, getHeight() / 2), //lower right
        new Point2D.Double(width - getWidth() / 2, height - getHeight() / 2), //upper right
        new Point2D.Double(getWidth() / 2, getHeight() / 2), //lower left
       };
    int i = 0;
    moveToPoint(getCenter());
    //int hitTimes = 0;
    while (true) {
      if (i > 3) {
        i = 0;
        //turnRadarRight(300);
      }
      moveToPoint(locs[i]);
      if (i % 2 == 0) {
      turnGunRight(-40);
      }
      else {
      turnGunRight(40);
      }
      i++;
    }
  }
  
  /**
   * Robot's reaction when it saw an enemy.
   * @param e the system event of onScannedRobot
   * */
  public void onScannedRobot(ScannedRobotEvent e) {
    /*
    double power = 0;
    if(e.getDistance() < 100){
      power = 3;
    }
    else
    {
      power = 1;
    }
    double bulletVelocity = 20 - (3*power);
    double travelTime = e.getDistance()/bulletVelocity;
    double enemyTravelDist = travelTime*e.getVelocity();
    double enemyX = getX()+e.getDistance()*Math.tan(e.getBearingRadians());
    double enemyY = getY()+e.getDistance()*Math.tan(Math.PI/2-e.getBearingRadians());
    
    Point2D enemyNewLocation = 
    new Point2D.Double(enemyX+enemyTravelDist*Math.tan(e.getHeadingRadians()),enemyY
    +enemyTravelDist*Math.tan(Math.PI/2-e.getHeadingRadians()));
    fireAt(enemyNewLocation, power);*/

    if (e.getDistance() < 100) {
      double absoluteBearing = getHeading() + e.getBearing();
      turnGunRight(absoluteBearing - getGunHeading());
      fire(3);
      scan();
    }
    else {
      if (e.getDistance() < 500 && getEnergy() > 50) {
        double absoluteBearing = getHeading() + e.getBearing();
        turnGunRight(absoluteBearing - getGunHeading());
        fire(2); 
      }
    }
  }
  
  /**
   * Robot's reaction when it hit by an enemy.
   * @param e the system event of onHitRobot
   * */
  public void onHitRobot(HitRobotEvent e) {
    double absoluteBearing = getHeading() + e.getBearing();
    turnGunRight(absoluteBearing - getGunHeading());

    scan();
  }
  
  /**
   * Robot's reaction when it hit by a bullet.
   * @param e the system event of onHitByBullet
   * */
  public void onHitByBullet(HitByBulletEvent e) {
    double absoluteBearing = getHeading() + e.getBearing();
    turnGunRight(absoluteBearing - getGunHeading());

    //scan();
  }
}
